package cc.rengu.igas.momp.biz.service.impl;

import cc.rengu.igas.momp.common.enums.MompTxnNumEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.QueryPublicInfoService;
import cc.rengu.igas.momp.facade.request.QueryCashierCashbackBonusRuleRequest;
import cc.rengu.igas.momp.facade.request.QueryInstitutionMcAccountInfoRequest;
import cc.rengu.igas.momp.facade.response.QueryCashierCashbackBonusRuleResponse;
import cc.rengu.igas.momp.facade.response.QueryInstitutionMcAccountInfoResponse;
import cc.rengu.igas.momp.facade.result.Result;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;

/**
 * 公共信息查询接口Dubbo实现
 *
 * @author Jinan Liu
 * @version 1.0.0
 * @date 2020/6/14 10:54
 */
@SuppressWarnings("unchecked")
public class QueryPublicInfoServiceImpl implements QueryPublicInfoService {

    /**
     * 收银员返现积分规则查询接口Dubbo实现
     *
     * @param queryCashierCashbackBonusRuleRequest 收银员返现积分规则查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryCashierCashbackBonusRuleResponse> 收银员返现积分规则查询接口应答对象
     * @author Jinan Liu
     * @date 2020/4/2 21:18
     */
    @Override
    public Result<QueryCashierCashbackBonusRuleResponse> queryCashierCashbackBonusRule(QueryCashierCashbackBonusRuleRequest queryCashierCashbackBonusRuleRequest) {

        DubboService<QueryCashierCashbackBonusRuleRequest> dubboService = new DubboServiceImpl();
        Result<QueryCashierCashbackBonusRuleResponse> queryCashierCashbackBonusRuleResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.CASHIER_CASHBACK_BONUS_RULE_QUERY.getTxnNum().equals(queryCashierCashbackBonusRuleRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryCashierCashbackBonusRuleResponse> dubboResult = dubboService.callAtomService(queryCashierCashbackBonusRuleRequest,
                    MompTxnNumEnum.CASHIER_CASHBACK_BONUS_RULE_QUERY.getService(), QueryCashierCashbackBonusRuleResponse.class);
            queryCashierCashbackBonusRuleResponseResult.setResult(dubboResult.getResult());
            queryCashierCashbackBonusRuleResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryCashierCashbackBonusRuleResponse queryCashierCashbackBonusRuleResponse = new QueryCashierCashbackBonusRuleResponse();
            queryCashierCashbackBonusRuleResponseResult.setResult(queryCashierCashbackBonusRuleResponse);
            queryCashierCashbackBonusRuleResponse.setHeader(queryCashierCashbackBonusRuleRequest.getHeader());
            queryCashierCashbackBonusRuleResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryCashierCashbackBonusRuleResponse.setRespCode(((BizException) e).getCode());
                queryCashierCashbackBonusRuleResponse.setRespDesc(e.getMessage());
            } else {
                queryCashierCashbackBonusRuleResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryCashierCashbackBonusRuleResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }


        return queryCashierCashbackBonusRuleResponseResult;
    }

    /**
     * 法人机构营销活动账号查询接口Dubbo实现
     *
     * @param queryInstitutionMcAccountInfoRequest 法人机构营销活动账号查询接口请求对象
     * @return cc.rengu.igas.momp.facade.result.Result<cc.rengu.igas.momp.facade.response.QueryInstitutionMcAccountInfoResponse> 法人机构营销活动账号查询接口应答对象
     * @author Jinan Liu
     * @date 2020/5/29 16:53
     */
    @Override
    public Result<QueryInstitutionMcAccountInfoResponse> queryInsititutionMcAccountInfo(QueryInstitutionMcAccountInfoRequest queryInstitutionMcAccountInfoRequest) {

        DubboService<QueryInstitutionMcAccountInfoRequest> dubboService = new DubboServiceImpl();
        Result<QueryInstitutionMcAccountInfoResponse> queryInstitutionMcAccountInfoResponseResult = new Result<>();
        try {
            if (!MompTxnNumEnum.QUERY_INSTITUTION_MC_ACCOUNT.getTxnNum().equals(queryInstitutionMcAccountInfoRequest.getHeader().getTxnNum())) {
                throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
            }
            // 通过服务名调用相应的服务
            DubboResult<QueryInstitutionMcAccountInfoResponse> dubboResult = dubboService.callAtomService(queryInstitutionMcAccountInfoRequest,
                    MompTxnNumEnum.QUERY_INSTITUTION_MC_ACCOUNT.getService(), QueryInstitutionMcAccountInfoResponse.class);
            queryInstitutionMcAccountInfoResponseResult.setResult(dubboResult.getResult());
            queryInstitutionMcAccountInfoResponseResult.setSuccess(dubboResult.isSuccess());

        } catch (Exception e) {

            QueryInstitutionMcAccountInfoResponse queryInstitutionMcAccountInfoResponse = new QueryInstitutionMcAccountInfoResponse();
            queryInstitutionMcAccountInfoResponseResult.setResult(queryInstitutionMcAccountInfoResponse);
            queryInstitutionMcAccountInfoResponse.setHeader(queryInstitutionMcAccountInfoRequest.getHeader());
            queryInstitutionMcAccountInfoResponseResult.setSuccess(false);
            if (e instanceof BizException) {
                queryInstitutionMcAccountInfoResponse.setRespCode(((BizException) e).getCode());
                queryInstitutionMcAccountInfoResponse.setRespDesc(e.getMessage());
            } else {
                queryInstitutionMcAccountInfoResponse.setRespCode(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                queryInstitutionMcAccountInfoResponse.setRespDesc(OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
            }
        }


        return queryInstitutionMcAccountInfoResponseResult;
    }
}
